home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: newing char**
- Date: 13 Feb 1996 01:59:20 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Message-ID: <4for9o$iv8@colossus.holonet.net>
- References: <4fggal$4pf@darkstar.UCSC.EDU> <4fmgi5$jvv@darkstar.UCSC.EDU>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Jonathan Gibbs (jono@cse.ucsc.edu) wrote:
- : Thanks for the replies. Unfortunatly I made a few typos in the post.
- : The solution was change
- : name = new (char*)[num];
- : to
- : name = new char*[num];
-
- : Does anyone know if the () are legal, though?
-
- The expression on the rhs is legal with parens, but it does not
- do what you think! Let's look at what it means:
-
- new (char*)[num]
-
- can be written
-
- new(char*) [num]
-
- which is the same as
-
- ( new char* )[num]
-
- which is the same as
-
- *(new char* + num)
-
- This ridiculous but legal expression allocates a char* on
- the heap, then accesses unallocated storage at a byte offset
- num * sizeof(char*) from this heap location. Mayhem will
- ensue!
-
- Fortunately, since I believe you declared name with the type
- char**, and the rhs has type char*, the above assignment would
- not be legal and you should get a compile error.
- --
- Russell Blackadar, russell@mdli.com
-